'use strict';

const Service = require('egg').Service;
const ulid = require('../public/ulid');
const ConnectOption = require('../oltmodules/ConnectOption');
const ZTEOLT = require('../oltmodules/ZTEOLT');
const HWOLT = require('../oltmodules/HuaWeiOLT');
const XDOLT = require('../oltmodules/XinDeOLT');
const XGLOLT = require('../oltmodules/XinGeLinOLT');
class SpeedProfileService extends Service {
  async list(params) {
    let result = {
        count:"",
        rows:""
    }
    const where = {};
    where.oltId = params.OltId
    if(params.search){
      where.name = new RegExp(params.search)
    }
    // let oltResult = await this.ctx.model.Olt.findOne({_id:params.OltId})
    result.count = await this.ctx.model.SpeedProfile.find(where).count('_id')
    // if(oltResult.oltType == "HUAWEI"){
      // result.rows = await this.ctx.model.SpeedProfile.find({oltId:params.OltId}).sort('index').skip(parseInt((params.page - 1) * params.limit)).limit(parseInt(params.limit))
    // }else{
      result.rows = await this.ctx.model.SpeedProfile.find(where).collation({"locale": "zh", numericOrdering:true}).sort({name:1}).skip(parseInt((params.page - 1) * params.limit)).limit(parseInt(params.limit))
    // }
    return result
  }
  async allList(params) {
    let result = await this.ctx.model.SpeedProfile.find({oltId:params.OltId}).collation({"locale": "zh", numericOrdering:true}).sort({name:1})
    return result
  }
  async create(params) {
    const result = {
      msg: '',
      flag:false
    };
    try {
      let oltResult = await this.ctx.model.Olt.findOne({_id:params.oltId})
      if(oltResult.oltType == "ZTE"){
        if(params.CBS == ""){
          params.CBS = "default"
        }
        if(params.PBS == ""){
          params.PBS = "default"
        }
      }else if(oltResult.oltType == "HUAWEI"){
        if(params.CBS == ""){
          params.CBS = parseInt(params.SIR)*32+2000
        }
        if(params.PBS == ""){
          params.PBS = parseInt(params.PIR)*32+2000
        }
      }
      await speedProfileCreate(oltResult,params,this)
      let speedProfileResult = await this.ctx.model.SpeedProfile.findOne({oltId:params.oltId,name:params.name})
      if(speedProfileResult){
        await this.ctx.model.SpeedProfile.updateOne({_id:speedProfileResult._id},{$set:{name:params.name,description:params.description,PIR:params.PIR,SIR:params.SIR,PBS:params.PBS,CBS:params.CBS}})
      }else{
        await this.ctx.model.SpeedProfile.create({_id:ulid.ulid(),name:params.name,description:params.description,PIR:params.PIR,SIR:params.SIR,PBS:params.PBS,CBS:params.CBS,oltId:params.oltId})
      }
      result.msg = "create speedprofile successful"
      result.flag = true
      return result
    } catch (error) {
      result.msg = "create speedprofile fail,"+error
      result.flag = false
      return result
    }
  }
  async delete(params) {
    const result = {
      msg: '',
      flag:false
    };
    try {
      let speedProfileResult = await this.ctx.model.SpeedProfile.findOne({_id:params.id})
      let oltResult = await this.ctx.model.Olt.findOne({_id:speedProfileResult.oltId})
      await speedProfileDelete(speedProfileResult,oltResult,this)
      await this.ctx.model.SpeedProfile.deleteOne({_id:params.id})
      result.msg = "delete speedprofile successful"
      result.flag = true
      return result
    } catch (error) {
      result.msg = "delete speedprofile fail,"+error
      result.flag = false
      return result
    }
  }
  async edit(params) {
    let result = {
      msg:"",
      flag:false
    }
    try {
      let speedResult = await this.ctx.model.SpeedProfile.findOne({_id:params.id})
      let oltResult = await this.ctx.model.Olt.findOne({_id:speedResult.oltId})
      if(oltResult.oltType == "ZTE"){
        if(params.CBS == ""){
          params.CBS = "default"
        }
        if(params.PBS == ""){
          params.PBS = "default"
        }
      }else if(oltResult.oltType == "HUAWEI"){
        if(params.CBS == ""){
          params.CBS = parseInt(params.SIR)*32+2000
        }
        if(params.PBS == ""){
          params.PBS = parseInt(params.PIR)*32+2000
        }
      }
      await speedProfileEdit(oltResult,params,this)
      await this.ctx.model.SpeedProfile.updateOne({_id:params.id},{$set:{description:params.description,SIR:params.SIR,PIR:params.PIR,CBS:params.CBS,PBS:params.PBS}})
      result.msg = "edit speedprofile successful"
      result.flag = true
      return result
    } catch (error) {
      result.msg = "edit speedprofile fail,"+error
      result.flag = false
      return result
    }
  }
}
async function speedProfileCreate(oltInformation,params,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        let zte = new ZTEOLT(options)
        result = await zte.speedProfileCreate(params.name,params.PIR,params.SIR,params.CBS,params.PBS,oltInformation.oltHardVersion,me)
        break;
      case "HUAWEI":
        let hw = new HWOLT(options)
        result = await hw.speedProfileCreate(params.name,params.PIR,params.SIR,params.CBS,params.PBS,me)
        break;
    }
    return result
  } catch (error) {
    throw(error)
  }
}
async function speedProfileDelete(speedProfileResult,oltInformation,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        let zte = new ZTEOLT(options)
        result = await zte.speedProfileDelete(speedProfileResult.name,oltInformation.oltHardVersion,me)
        break;
      case "HUAWEI":
        let hw = new HWOLT(options)
        result = await hw.speedProfileDelete(speedProfileResult.name,me)
        break;
    }
    return result
  } catch (error) {
    throw error
  }
}
async function speedProfileEdit(oltInformation,params,me){
  try {
    const options = new ConnectOption(oltInformation.oltIP, oltInformation.initCommunity, oltInformation.snmpUDPPort, 162, true, true, oltInformation.telnetTCPPort, oltInformation.oltManagementPassword, oltInformation.oltTelnetUsername, oltInformation.oltTelnetPassword, oltInformation.systemName,oltInformation.RZUsername,oltInformation.RZPassword,oltInformation.telnetProxySwitch);
    let result
    switch (oltInformation.oltType) {
      case "ZTE":
        let zte = new ZTEOLT(options)
        result = await zte.speedProfileEdit(params.name,params.PIR,params.SIR,params.CBS,params.PBS,oltInformation.oltHardVersion,me)
        break;
      case "HUAWEI":
        let hw = new HWOLT(options)
        result = await hw.speedProfileEdit(params.name,params.PIR,params.SIR,params.CBS,params.PBS,me)
        break;
    }
    return result
  } catch (error) {
    throw(error)
  }
}

module.exports = SpeedProfileService;
